package com.haha.community.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Yang
 * @date 2020/10/31
 */
public class CookieUtil {
    //从请求中得到特定的cookie值
    public static String getValue(HttpServletRequest request,String name){
        Cookie[] cookies = request.getCookies();
        if (request==null||name==null){
            throw new IllegalArgumentException("请求的request和name都不能为空");
        }
        if (cookies==null){
            return null;
        }
        for (Cookie cookie : cookies) {
            if (StringUtils.equals(cookie.getName(),name)){
                return cookie.getValue();
            }
        }
        return null;

    }
}
